package gui;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Stage;
import agents.MachineAgent;

public class MachineGui extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	MachineAgent agent;
	
	public MachineGui(MachineAgent agent) {
		super(agent.getLocalName());
		this.agent= agent;
		initComponents();
		
		
	}
	
	
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    private JLabel jLabel8;
    private JPanel jPanel1;
	public void initComponents(){

	        jLabel1 = new javax.swing.JLabel();
	        jLabel2 = new javax.swing.JLabel();
	        jLabel3 = new javax.swing.JLabel();
	        jLabel4 = new javax.swing.JLabel();
	        jLabel5 = new javax.swing.JLabel();
	        jLabel6 = new javax.swing.JLabel();
	        jLabel7 = new javax.swing.JLabel();
	        jLabel8 = new javax.swing.JLabel();
	        jPanel1 = new javax.swing.JPanel();
	        jButton1 = new javax.swing.JButton();
	        jButton2 = new javax.swing.JButton();

	        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

	        jLabel1.setText("name:");
	        
	        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
	        jLabel2.setText(agent.getLocalName());

	        jLabel3.setText("position:");

	        jLabel4.setText("(" + agent.getPosition().getX() + ";" + agent.getPosition().getY() + ")");

	        jLabel5.setText("product:");

	        jLabel6.setText("no current product");

	        jLabel7.setText("type:");

	        jLabel8.setText(agent.getType().toString());

	        jButton1.setText("Debug");
	        jButton1.setActionCommand("debug");
	        jButton1.addActionListener(this);
	        
	        jButton2.setText("New Product");
	        jButton2.setActionCommand("new product");
	        jButton2.addActionListener(this);
			
	        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
	        jPanel1.setLayout(jPanel1Layout);
			if(agent.getType() == Stage.init)
			{
	        jPanel1Layout.setHorizontalGroup(
	            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(jPanel1Layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addComponent(jButton2)
	                    .addGroup(jPanel1Layout.createSequentialGroup()
	                        .addGap(14, 14, 14)
	                        .addComponent(jButton1)))
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
			}else{
		        jPanel1Layout.setHorizontalGroup(
			            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			            .addGroup(jPanel1Layout.createSequentialGroup()
			                .addContainerGap()
			                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			                    .addGroup(jPanel1Layout.createSequentialGroup()
			                        .addGap(14, 14, 14)
			                        .addComponent(jButton1)))
			                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
			        );
			}
			if(agent.getType() == Stage.init)
			{
		        jPanel1Layout.setVerticalGroup(
			            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			            .addGroup(jPanel1Layout.createSequentialGroup()
			                .addGap(22, 22, 22)
			                .addComponent(jButton1)
			                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
			                .addComponent(jButton2)
			                .addContainerGap(21, Short.MAX_VALUE))
			        );
			}else{
		        jPanel1Layout.setVerticalGroup(
			            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			            .addGroup(jPanel1Layout.createSequentialGroup()
			                .addGap(22, 22, 22)
			                .addComponent(jButton1)
			                .addContainerGap(55, Short.MAX_VALUE))
			        );
			}
			
	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jLabel1)
	                            .addComponent(jLabel7)
	                            .addComponent(jLabel3))
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
	                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                    .addGroup(layout.createSequentialGroup()
	                        .addComponent(jLabel5)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                .addContainerGap(23, Short.MAX_VALUE))
	            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel1)
	                    .addComponent(jLabel2))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel7)
	                    .addComponent(jLabel8))
	                .addGap(8, 8, 8)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel3)
	                    .addComponent(jLabel4))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(jLabel5)
	                    .addComponent(jLabel6))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
	                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap())
	        );

	        pack();
	}
	
	public void showGui() {
		pack();
		super.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
	    if ("debug".equals(e.getActionCommand())) {
	    	System.out.println(agent.toString());
	    } else if("new product".equals(e.getActionCommand())) {
	    	if(agent.hasProduct())
	    	{
	    		ErrorMessageGui error = new ErrorMessageGui("Machine " + agent.getLocalName() + " already has a product waiting for pickup");
	    		error.showGui();
	    	}
	    	else{
	    		NewProductGui newProductGui = new NewProductGui(agent);
	    		newProductGui.showGui();
	    	}
	    }
	}

	public void setProductName(String name) {
		jLabel6.setText(name);
		//jButton2.setEnabled(false);
	}

}